home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
8206
/
8206.xpi
/
content
/
rss
/
xmlrpc.js
< prev
Wrap
Text File
|
2010-02-02
|
7KB
|
294 lines
function getInnerText (node) {
if (typeof node.textContent != 'undefined') {
return node.textContent;
} else if (typeof node.innerText != 'undefined') {
return node.innerText;
} else if (typeof node.text != 'undefined') {
return node.text;
} else {
switch (node.nodeType) {
case 3:
case 4:
return node.nodeValue;
break;
case 1:
case 11:
var innerText = '';
for (var i = 0; i < node.childNodes.length; i++) {
innerText += getInnerText(node.childNodes[i]);
}
return innerText;
break;
default:
return '';
}
}
}
WiseStampXMLRPCResponse.prototype = new Object;
WiseStampXMLRPCResponse.prototype.isError = WiseStampXMLRPCResponse_isError;
function WiseStampXMLRPCResponse(param) {
this.param = param;
}
function WiseStampXMLRPCResponse_isError() {
return false;
}
/* ---------- */
WiseStampXMLRPCError.prototype = new Object;
WiseStampXMLRPCError.prototype.isError = WiseStampXMLRPCError_isError;
WiseStampXMLRPCError.prototype.getMessage = WiseStampXMLRPCError_getMessage;
function WiseStampXMLRPCError(code, message) {
this._message = message;
this._code = code;
}
function WiseStampXMLRPCError_getMessage() {
return this._message;
}
function WiseStampXMLRPCError_isError() {
return true;
}
/* ---------- */
var WiseStampXMLRPC = new Object;
WiseStampXMLRPC.prepare = function(name, params) {
var methodName = "<methodName>"+name+"</methodName>\n";
var paramsText = "<params>\n";
for (var i=0; i<params.length; i++) {
var paramText = "<param><value><string>"+params[i]+"</string></value></param>\n";
paramsText += this._prepareParam(params[i]);
};
paramsText += "</params>\n";
return "<methodCall>\n"+methodName+paramsText+"</methodCall>\n";
};
WiseStampXMLRPC._prepareParam = function(value) {
var paramText;
paramText = "<param>";
paramText += this._prepareValue(value);
paramText += "</param>\n";
return paramText;
}
WiseStampXMLRPC._prepareValue = function(value) {
var paramText = "<value>";
switch (typeof value) {
case "object":
paramText += this._prepareObject(value);
break;
default:
paramText += this._prepareString(value);
break;
};
paramText += "</value>";
return paramText;
}
WiseStampXMLRPC._prepareObject = function(value) {
var valueText = "";
for (var key in value) {
valueText += "<member><name>"+key+"</name>"+this._prepareValue(value[key])+"</member>\n"
};
return "<struct>"+valueText+"</struct>";
}
WiseStampXMLRPC._prepareString = function(value) {
return "<string>"+value.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<")+"</string>";
}
WiseStampXMLRPC.parseResponse = function(responseXML) {
// Check if it is the error response
var faultElement = responseXML.getElementsByTagName('fault')[0];
// TODO: make more reliable parsing utility
if (faultElement) {
var code = getInnerText(faultElement.getElementsByTagName('int')[0]);
var message = getInnerText(faultElement.getElementsByTagName('string')[0]);
var response = new WiseStampXMLRPCError(code, message);
return response;
};
// Normal response
//
// The body of the response is a single XML structure, a <methodResponse>,
// which _can_ contain a single <params> which contains a single <param> which contains a single <value>.
//
var paramXML = responseXML.getElementsByTagName('param')[0];
if (paramXML) {
return new WiseStampXMLRPCResponse(this._parseResponseParam(paramXML));
} else {
return new WiseStampXMLRPCResponse(null);
};
};
WiseStampXMLRPC._parseResponseParam = function(element) {
for (var i=0; i<element.childNodes.length; i++) {
var node = element.childNodes[i];
if (node.tagName == "value") {
return this._parseValue(node);
};
};
}
WiseStampXMLRPC._parseValue = function(element) {
for (var i=0; i<element.childNodes.length; i++) {
var node = element.childNodes[i];
switch (node.tagName) {
case "int":
case "i4":
return this._parseInt(node);
case "boolean":
return this._parseBoolean(node);
case "string":
return this._parseString(node);
case "double":
return this._parseDouble(node);
case "dateTime.iso8601":
return this._parseDateTime(node);
case "base64":
return this._parseBase64(node);
case "struct":
return this._parseStruct(node);
case "array":
return this._parseArray(node);
};
};
}
WiseStampXMLRPC._parseInt = function(element) {
return parseInt(getInnerText(element));
}
WiseStampXMLRPC._parseBoolean = function(element) {
var text = getInnerText(element);
if (!text) {
return false;
};
if (text == 'false') {
return false;
};
if (text == 'no') {
return false;
};
if (text == '0') {
return false;
};
return true;
}
WiseStampXMLRPC._parseString = function(element) {
return getInnerText(element);
}
WiseStampXMLRPC._parseDouble = function(element) {
return parseFloat(getInnerText(element));
}
WiseStampXMLRPC._parseDateTime = function(element) {
// @TODO
return getInnerText(element);
}
WiseStampXMLRPC._parseBase64 = function(element) {
return getInnerText(element);
}
WiseStampXMLRPC._parseStruct = function(element) {
var struct = new Object;
for (var i=0; i<element.childNodes.length; i++) {
var node = element.childNodes[i];
if (node.tagName == "member") {
var member = this._parseMember(node);
struct[member.name] = member.value;
};
};
return struct;
}
WiseStampXMLRPC._parseMember = function(element) {
var member = new Array();
for (var i=0; i<element.childNodes.length; i++) {
var node = element.childNodes[i];
switch (node.tagName) {
case "name":
member.name = this._parseName(node);
break;
case "value":
member.value = this._parseValue(node);
break;
};
};
return member;
}
WiseStampXMLRPC._parseName = function(element) {
return getInnerText(element);
}
WiseStampXMLRPC._parseArray = function(element) {
for (var i=0; i<element.childNodes.length; i++) {
var node = element.childNodes[i];
if (node.tagName == "data") {
return this._parseData(node);
};
};
}
WiseStampXMLRPC._parseData = function(element) {
var data = new Array();
for (var i=0; i<element.childNodes.length; i++) {
var node = element.childNodes[i];
if (node.tagName == "value") {
data.push(this._parseValue(node));
};
};
return data;
}
WiseStampXMLRPC.call = function(url, successCallback, failureCallback, name, params) {
var request = WiseStampCreateXMLHttpRequest();
request.error = WiseStampXMLRPC._ajaxErrorHandler;
request.onsuccess = function() {
if (request.handleError()) {
if (failureCallback) {
failureCallback();
};
return false;
};
var response = WiseStampXMLRPC.parseResponse(request.xmlhttp.responseXML);
successCallback(response);
};
request.open('POST',url,1);
// request.xmlhttp.setRequestHeader("Content-Type", "text/xml");
var query = this.prepare(name, params);
request.send(query);
};
WiseStampXMLRPC._ajaxErrorHandler = function(text) {
alert(text);
return true;
};